package com.qfedu.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.qfedu.fmmall.config.ObjectMapperUtil;
import com.qfedu.fmmall.entity.IndexImg;
import com.qfedu.fmmall.mapper.IndexImgMapper;
import com.qfedu.fmmall.service.IndexImgService;
import com.qfedu.fmmall.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 轮播图  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService {
    @Resource
    private IndexImgMapper indexImgMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVO listIndexImgs() {
//        缓存获取轮播图信息
        String indexImgs1 = stringRedisTemplate.opsForValue().get("indexImgs");
        if (indexImgs1 != null && !("null".equals(indexImgs1))) {
            try {
                ArrayList list = ObjectMapperUtil.getObjectMapper().readValue(indexImgs1, ArrayList.class);
                return ResultVO.success(list, "成功！");
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        } else {
//            加锁并发时，造成缓存击穿
            synchronized (this) {
                //        缓存获取轮播图信息
                String indexImgss = stringRedisTemplate.opsForValue().get("indexImgs");
                if (indexImgss == null) {
//                    在查询数据库，并把数据缓存到redis中
                    List<IndexImg> indexImgs = indexImgMapper.selectList(
                            new LambdaQueryWrapper<IndexImg>().eq(IndexImg::getStatus, 1).orderBy(true, true, IndexImg::getSeq));
                    if (indexImgs != null) {
                        try {
//                 存入缓存中
                            stringRedisTemplate.opsForValue().set("indexImgs", ObjectMapperUtil.getObjectMapper().writeValueAsString(indexImgs));
                            stringRedisTemplate.expire("indexImgs", 5, TimeUnit.DAYS);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e);
                        }
                        return ResultVO.success(indexImgs, "成功！");
                    } else {
                        stringRedisTemplate.opsForValue().set("indexImgs", "null");
                        stringRedisTemplate.expire("indexImgs", 2, TimeUnit.MINUTES);
                        return ResultVO.error("遇到问题！");
                    }
                } else {
                    ArrayList list = null;
                    try {
                        list = ObjectMapperUtil.getObjectMapper().readValue(indexImgs1, ArrayList.class);
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                    return ResultVO.success(list, "成功！");
                }
            }
        }
    }
}
